/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	VertexShaderStage.cpp
*
*	Comments	-	See VertexShaderStage.h
*
**************************************************************************************/
#include "../Include/PixelShaderStage.h"
#include "../Include/Renderer.h"
#include "../Include/PixelShader.h"

namespace Pulse
{
	PixelShaderStage::PixelShaderStage( void )
		: IShaderStage()
	{

	}

	PixelShaderStage::~PixelShaderStage( void )
	{

	}

	void PixelShaderStage::ClearStates( void )
	{
		m_stateCache.ClearStates();
	}

	void PixelShaderStage::BindStates( void )
	{
		BindShader();
		BindConstantBuffers();
		BindShaderResourceViews();
		BindSamplerStates();
		//BindUnordererdAccessViews();
	}

	void PixelShaderStage::BindShader( void )
	{
		ID3D11DeviceContext *pDC = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();

		if ( m_stateCache.m_pShader )
		{
			ID3D11PixelShader *pShader = (ID3D11PixelShader*)m_stateCache.m_pShader->GetNakedShader();

			if ( m_stateCache.m_pShader->GetShaderType() == EShader::PIXEL )
				pDC->PSSetShader( pShader, 0, 0 );
			else
				PSX_PushError( "Invalid Shader type to bind. Must specify a pixel shader type." );
		}
		else
		{
			pDC->PSSetShader( PSX_NULL, 0, 0 );
		}
	}
	
	void PixelShaderStage::BindConstantBuffers( void )
	{
		ID3D11DeviceContext *pDC = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();
		pDC->PSSetConstantBuffers( 0, PSX_NUM_CONSTANT_BUFFER_SLOT, (ID3D11Buffer * const *)m_stateCache.m_pD3DConstantBuffers );
	}
	
	void PixelShaderStage::BindSamplerStates( void )
	{
		ID3D11DeviceContext *pDC = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();
		pDC->PSSetSamplers( 0, PSX_NUM_SAMPLER_SLOT, (ID3D11SamplerState * const *)m_stateCache.m_pD3DSamplerStates );
	}
	
	void PixelShaderStage::BindShaderResourceViews( void )
	{
		ID3D11DeviceContext *pDC = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();
		pDC->PSSetShaderResources( 0, PSX_NUM_INPUT_RESOURCE_SLOT, (ID3D11ShaderResourceView * const *)m_stateCache.m_pD3DShaderResources );
	}

	//void PixelShaderStage::BindUnordererdAccessViews( void )
	//{
	//	// NOTE: We need to fix this once we're using append/consume buffers.
	//	const UINT initialCounts[PSX_NUM_UAV_SLOT] = { -1 }; // Unused.

	//	ID3D11DeviceContext *pDC = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();
	//	pDC->PSSet
	//	pDC->CSSetUnorderedAccessViews( 0, PSX_NUM_UAV_SLOT, (ID3D11UnorderedAccessView * const *)m_stateCache.m_pD3DUnorderedAccessViews, initialCounts );
	//}
}